﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using System.Text;

namespace DotNETX.Authentication.Db
{
    public class SqlServerTokenManager : DbTokenManager
    {

        public override string CreateParam(IDbCommand cmd, string paramName, object paramValue)
        {
            return DotNETX.Authentication.Db.SqlServer.ParameterUtils.CreateParam(cmd, paramName, paramValue);
        }
        public override bool TryOpenConnection(IDbConnection con)
        {
            return base.TryOpenConnection(con);
        }

        public static SqlServerTokenManager Create(string datasource, string database, string uid, string pwd, string table, string token_col, string uniqueid_col, string logintime_col, string lastvisittime_col, string status_col = null)
        {
            IDbConnection con = SqlServer.ConnectionUtils.CreateSqlConnection(datasource, database, uid, pwd);
            return Create(con, table, token_col, uniqueid_col, logintime_col, lastvisittime_col, status_col);
        }

        public static SqlServerTokenManager Create(string connStr, string table, string token_col, string uniqueid_col, string logintime_col, string lastvisittime_col, string status_col = null)
        {
            IDbConnection con = SqlServer.ConnectionUtils.CreateSqlConnection(connStr);
            return Create(con, table, token_col, uniqueid_col, logintime_col, lastvisittime_col, status_col);
        }

        public static SqlServerTokenManager Create(IDbConnection conn, string table, string token_col, string uniqueid_col, string logintime_col, string lastvisittime_col, string status_col = null)
        {
            return new SqlServerTokenManager()
            {
                Connection = conn,
                TableName = table,
                TokenColumn = token_col,
                UniqueIdColumn = uniqueid_col,
                LoginTimeColumn = logintime_col,
                LastVisitTimeColumn = lastvisittime_col,
                StatusColumn = status_col
            };
        }


    }
}
